<?php

namespace app\home\controller;

use app\common\model\Cart;
use app\common\model\OrderData;
use think\Db;

class Order extends Common
{
    
    public $uid;
    
    public function initialize()
    {
        parent::initialize();
        if (!session('uid')) {
            header('location:' . url('/login'));
        }
        $this->uid = session('uid');
    }
    
    public function checkout(Cart $cartMd)
    {
        $list = $cartMd->where('uid', $this->uid)->select();
        $total = $cartMd->where('uid', $this->uid)->sum('price');
        $total = bcmul($total, 1, 2);

        $region_list = Db("region_au")->where('parent_id',  0)->select();
        $first_region = $region_list[0]??"";
        $region_list1 = Db("region_au")->where('parent_id',  $first_region['id'])->select();

        $this->assign('region_list', $region_list);
        $this->assign('region_list1', $region_list1);

//        dump($list);die;
        $this->assign('list', $list);
        $this->assign('total', $total);

        return $this->fetch();
    }

    public function region(Cart $cartMd)
    {
        $pid = $_POST['pid']??0;
        $pid = (int)$pid;
        $region_list = Db("region_au")->where('parent_id',  $pid)->select();
        $html = "";
        foreach ($region_list as $val){
            $html.= "<option value='".$val['id']."'>".$val['name']."</option>";
        }

        return outJson(1,'ok',$html);
    }
    
    public function done(Cart $cartMd, \app\common\model\Order $order, OrderData $orderData)
    {
        /* 检查购物车中是否有商品 */
        $cart_count = $cartMd->where('uid', $this->uid)->count();
        if ($cart_count == 0) {
            $this->error('No Product!');
        }
        $user = Db("users")->where("id",$this->uid)->find();
        /*商品金额*/
        $cart = $cartMd->where('uid', $this->uid)->select()->toArray();
        $total = $cartMd->where('uid', $this->uid)->sum('price');
        $o_data = input('post.');

        if(empty($o_data['state'])){
            $this->error('Please select state');
        }
        if(empty($o_data['city'])){
            $this->error('Please select city');
        }

        $region_state = Db("region_au")->where('id',$o_data['state'])->where("level",0)->find();
        if(empty($region_state)){
            $this->error('state not exist');
        }
        $o_data['state'] = $region_state['name'];

        $region_city = Db("region_au")->where('id',$o_data['city'])->where("level",1)->find();
        if(empty($region_city)){
            $this->error('city not exist');
        }
        $o_data['city'] = $region_city['name'];
        $o_data['post_code'] = $region_city['post_code'];
        $o_data['goods_total'] = $total;
        $o_data['shipping_fee'] = 0;
        $o_data['order_total'] = bcadd($o_data['shipping_fee'], $o_data['goods_total'], 2);
        $o_data['uid'] = $this->uid;
        $o_data['create_time'] = time();
        $o_data['sn'] = 'T' . date('YmdHis') . rand(111, 999) . rand(111, 999)."_".$user['id']."_".$user['username'];
        $rs = $order->create($o_data);
        if ($rs) {
            $data = [];
            foreach ($cart as $v) {
                unset($v['id']);
                $v['create_time'] = time();
                $v['update_time'] = time();
                $v['oid'] = $rs->id;
                $data[] = $v;
            }
            $orderData->insertAll($data);
            $cartMd->where('uid', $this->uid)->delete();
            $this->success('Success', url('Home/User/index'));
        } else {
            $this->error('Error');
        }
    }
}